Uso de socket TCP para
avaliar rede Convolucional
Guilherme Ferreira de Jesus
Henrique Santos de Lima
A rede convolucional
1. #define CONV 1
2. #define POOL 2
3. #define RELU 3
4. #define DROPOUT 4
5. #define FULLCONNECT 5
6. typedef struct {
7. fvv calc_grads;
8. fv corrige_pesos;
9. fv ativa;
10. fsl salvar;
11. char type;
12. fv release;
13. Params *parametros;
14. Tensor gradsEntrada;
15. Tensor entrada;
16. Tensor saida;
17. char flag_releaseInput;
18. char flag_notlearn;
19. cl_command_queue queue;
20.} *Camada, Typecamada;
1. Camada createRelu(WrapperCL *cl, unsigned int inx, unsigned int iny, unsigned int
inz, Tensor entrada, GPU_ERROR *error) {
2. if (error->error)return NULL;
3. CamadaRelu c = (CamadaRelu) calloc(1, sizeof(TypecamadaRelu));
4. c->super.gradsEntrada = newTensor(cl->context, inx, iny, inz, error);
5. c->super.saida = newTensor(cl->context, inx, iny, inz, error);
6. c->super.entrada = entrada;
7. if (!entrada) {
8. c->super.entrada = newTensor(cl->context, inx, iny, inz, error);
9. c->super.flag_releaseInput = 1;
10. }
11. c->super.release = (fv) realeaseRelu;
12. c->super.ativa = (fv) ativaRelu;
13. c->super.calc_grads = (fvv) calc_gradsRelu;
14. c->super.corrige_pesos = (fv) corrige_pesosRelu;
15. c->super.type = RELU;
16. c->super.salvar = (fsl) salvarRelu;
17. c->kernelReluAtiva = new_Kernel(cl->program, "reluativa", 3, VOID_P, VOID_P, INT);
18. c->kernelReluCalcGrads = new_Kernel(cl->program, "relucalcgrad", 4, VOID_P,
VOID_P, VOID_P, INT);
19. return (Camada) c;
20. }
API para processamento paralelo usando a placa de vídeo (GPU)
Biblioteca para facilitar o uso da API openCL
Passos para processar na GPU com a openCL
Compilar função para codigo de maquina da gpu (kernel)
alocar memória na GPU e passar os valores de entrada
atribuir os argumentos da função kernel
executar
copiar resultados para memoria ram
Função Kernel
1. __kernel void sub(__global double *grad, __global double *saida, __global double
*target, int k0) {
2. int k = get_global_id(0) + k0;
3. grad[k] = saida[k] - target[k];
4. }
c->kernelsub = new_Kernel(cl->program, "sub", 4, VOID_P, VOID_P, VOID_P, INT);
Criando identificador para c
Criando memória
1. void fillTensor(Tensor t, cl_context context, size_t bytes, GPU_ERROR *error) {
2. t->data = clCreateBuffer(context, CL_MEM_READ_WRITE, bytes, NULL, &error->error);
3. ...
4. }
5. clEnqueueWriteBuffer(c->queue, targ->data, CL_TRUE, 0, targ->bytes, target, 0,
NULL, NULL);
1. kernel_run_recursive(&c->kernelsub,c->queue,targ->x * targ->y * targ-
>z,max_works,
2. &lastGrad->data,&c->camadas[c->size - 1]->saida->data, &targ-
>data);
Chamando kernel
Passando resultados para Memória RAM
1. clEnqueueReadBuffer(c->queue, targ->data, CL_TRUE, 0, sizeof(double), &c-
>normaErro, 0, NULL, NULL);
Interface Python e C
+
O código em C é compilado em uma dll (para windows)
e em um .so para linux
Em python é carregado a dll/so usando o modulo
ctypes
Descrição de parâmetros
Uso
Programa em java
Framework Processing
Interface
Socket em java como cliente TCP
Recebendo dados
Tratando os dados recebidos
Socket servidor TCP e interface com rede
convolucional utilizando script Python
Carregando rede Convolucional
Criando servidor
Recebendo imagem
Processando na GPU
Devolvendo resultados
Aplicação final
Projeto disponível em:
Rede convolucional em C (github.com): https://github.com/Xx220xX/Rede-convolucional